unit frmForeignEdit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters,
  cxContainer, cxEdit, dxSkinsCore, dxSkinOffice2007Black, cxLabel,
  cxGroupBox, AdvGlowButton, cxStyles, dxSkinscxPCPainter, cxCustomData,
  cxFilter, cxData, cxDataStorage, DB, cxDBData, cxGridLevel, cxClasses,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBAccess, Uni, MemDS, ActnList;

type
  TForeignEditFrame = class(TFrame)
    grpForeignList: TcxGroupBox;
    cxLabel1: TcxLabel;
    btnAdd: TAdvGlowButton;
    btnApply: TAdvGlowButton;
    btnDel: TAdvGlowButton;
    btnRefresh: TAdvGlowButton;
    AdvGlowButton1: TAdvGlowButton;
    tblForeign: TUniTable;
    dsForeign: TUniDataSource;
    grdList: TcxGrid;
    grdListView: TcxGridDBTableView;
    grdListLevel1: TcxGridLevel;
    ForeignActionList: TActionList;
    actAdd: TAction;
    actDel: TAction;
    actSave: TAction;
    actRefr: TAction;
    procedure grdListViewEditValueChanged(Sender: TcxCustomGridTableView;
      AItem: TcxCustomGridTableItem);
    procedure actSaveUpdate(Sender: TObject);
    procedure actDelUpdate(Sender: TObject);
    procedure actAddExecute(Sender: TObject);
    procedure actSaveExecute(Sender: TObject);
    procedure actRefrExecute(Sender: TObject);
  private
    { Private declarations }
  protected
    FDataChanged: boolean;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
  end;

implementation

{$R *.dfm}

constructor TForeignEditFrame.Create(AOwner: TComponent);
begin
  inherited;
  FDataChanged := False;
end;

procedure TForeignEditFrame.grdListViewEditValueChanged(
  Sender: TcxCustomGridTableView; AItem: TcxCustomGridTableItem);
begin
  FDataChanged := True;
end;

procedure TForeignEditFrame.actSaveUpdate(Sender: TObject);
begin
  if actSave.Enabled <> (FDataChanged and (tblForeign.State in [dsEdit, dsInsert])) then
    actSave.Enabled := (FDataChanged and (tblForeign.State in [dsEdit, dsInsert]));
end;

procedure TForeignEditFrame.actDelUpdate(Sender: TObject);
begin
  if actDel.Enabled <> ((grdListView.ViewData.RecordCount <> 0) and (grdListView.Focused))then
    actDel.Enabled := ((grdListView.ViewData.RecordCount <> 0) and (grdListView.Focused));
end;

procedure TForeignEditFrame.actAddExecute(Sender: TObject);
begin
  tblForeign.Append;
end;

procedure TForeignEditFrame.actSaveExecute(Sender: TObject);
begin
  tblForeign.Post;
  FDataChanged := False;
end;

procedure TForeignEditFrame.actRefrExecute(Sender: TObject);
var
  v_id: variant;
begin
  v_id := Null;
  if tblForeign.Active then
    v_id := tblForeign.Fields[0].Value;

  tblForeign.DisableControls;
  try
    tblForeign.Close;
    tblForeign.Open;
  finally
    if not VarIsNull(v_id) then
      tblForeign.Locate('id', v_id, []);

    tblForeign.EnableControls;
  end;
end;

end.
